package io.renren.modules.xiaoqu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;
import io.renren.modules.xiaoqu.entity.ComplainEntity;
import io.renren.modules.xiaoqu.entity.MaintenanceEntity;
import io.renren.modules.xiaoqu.service.ComplainService;
import io.renren.modules.xiaoqu.service.MainTenanceService;
import io.renren.modules.xiaoqu.service.impl.MainTenanceServiceImpl;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Api
@RestController
@RequestMapping("xiaoqu/maintenance")
public class MainTenanceController {

    @Autowired
    private MainTenanceService mainTenanceService;

    @Autowired
    private UserService userService;

    /**
     * 列表
     */

    @PostMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        params.replace("page", params.get("page").toString());
        params.replace("limit", params.get("limit").toString());
        PageUtils page = mainTenanceService.queryPage(params);
        return R.ok().put("page", page);
    }

        @GetMapping("/getWarrantyList")
    public R getWarrantyList(String name, String master_name, Integer status,int page,int pageSize){

        LambdaQueryWrapper<UserEntity> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),UserEntity::getUsername,name);
//            queryWrapper.like(StringUtils.isNotBlank(userName),UserEntity::getUsername,userName);

        List<UserEntity> userEntityList = userService.list(queryWrapper);

        List<Long> ids=new ArrayList<>();

        for (UserEntity item:userEntityList
        ) {
            ids.add(item.getUserId());
        }

//        LambdaQueryWrapper<ComplainEntity> queryWrapper2=new LambdaQueryWrapper<>();
//        queryWrapper2.like(StringUtils.isNotBlank(master_name),ComplainEntity::getMaster,master_name);


        //状态
        LambdaQueryWrapper<MaintenanceEntity> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(status!=null, MaintenanceEntity::getFankui,status);
        queryWrapper1.like(StringUtils.isNotBlank(master_name),MaintenanceEntity::getRepairName,master_name);
//        queryWrapper1.eq(master_name!=null, ComplainEntity::getMaster,master_name);
        queryWrapper1.in(MaintenanceEntity::getUserId,ids);

        //分页
        Page<MaintenanceEntity> pageUtil=new Page<>(page,pageSize);
        Page<MaintenanceEntity> maintenanceEntityPage = mainTenanceService.page(pageUtil, queryWrapper1);

        maintenanceEntityPage.getRecords().forEach(item->{
            if (item.getFankui().equals("0")) {
                item.setFankui("未维修");
            }else if (item.getFankui().equals("1")) {
                item.setFankui("已维修");
            }
            if (item.getIsback().equals("0")){
                item.setIsback("未回访");
            }else if (item.getIsback().equals("1")){
                item.setIsback("已回访");
            }
        });

            for (MaintenanceEntity item:maintenanceEntityPage.getRecords()
            ) {
                //根据userid查询用户名
                UserEntity byId = userService.getById(item.getUserId());
                item.setNumber(byId.getNumber());
                item.setAddress(byId.getAddress());
            }




        return R.ok().put("page", maintenanceEntityPage);
    }

    /**
     * 保存
     */
    @PostMapping("/save")

    public R save(@RequestBody MaintenanceEntity maintenanceEntity){

//        complain.setDate(new Date());

        mainTenanceService.save(maintenanceEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody MaintenanceEntity maintenanceEntity){
        mainTenanceService.updateById(maintenanceEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Integer[] ids){
        System.out.println(ids);
        mainTenanceService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @Login
    @GetMapping("/test")
    public R test(@LoginUser UserEntity user){
        return R.ok().put("data",user);
    }
}
